package view.admin;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;

import controller.TableHandler;
import controller.ActionEvents.ActionHandler;

public class AdminEditUser extends JPanel {

	
	private ActionHandler actionHandler;
	private JButton btnMainMenu;
	private JTable table;
	private DefaultTableModel model;
	private TableHandler tableHandler;
	private JScrollPane scrollPane;
	private JLabel lblEditUser;
	public String[] columnHeaders = {"ID", "Brugernavn", "Password", "Admin"};
	private Color tableColors = Color.WHITE;
	private Color tableShadeColors = Color.lightGray;
	
	/**
	 * Create the panel.
	 */
	public AdminEditUser(ActionHandler actionHandler, TableHandler tableHandler) {
		setBackground(Color.WHITE);
		setPreferredSize(new Dimension(750, 480));
		this.actionHandler = actionHandler;
		this.tableHandler = tableHandler;
		lblEditUser = new JLabel("Edit user");
		lblEditUser.setFont(new Font("Lucida Grande", Font.PLAIN, 28));
		lblEditUser.setBounds(327, 24, 130, 36);
		add(lblEditUser);
		
		btnMainMenu = new JButton("Main menu");
		btnMainMenu.setBounds(677, 451, 117, 29);
		btnMainMenu.setBorderPainted(false);
		btnMainMenu.setContentAreaFilled(false);
		btnMainMenu.addActionListener(actionHandler);
		setLayout(null);
		btnMainMenu.setActionCommand("backToAdminMenu");
		add(btnMainMenu);
		
		
		
		
	}
	public void removeTable(){
		if(table!=null)
		remove(table);
	}
	
	public void addTable()
	{
		model = new DefaultTableModel( actionHandler.getFrame().getModelClass().getUsersRows(), columnHeaders ) 
		{
			Class[] columnTypes = new Class[] 
			{
				String.class, String.class, String.class, Boolean.class //kolonne datatyper
			};
			public Class getColumnClass(int columnIndex) 
			{
				return columnTypes[columnIndex];
			}
			boolean[] columnEditables = new boolean[] 
			{
				false, true, true, true
			};
			public boolean isCellEditable(int row, int column) 
			{
				return columnEditables[column];
			}
		};
		
		table = new JTable(){
			public Component prepareRenderer(TableCellRenderer renderer,int Index_row, int Index_col) {
				Component comp = super.prepareRenderer(renderer, Index_row, Index_col);
				//even index, selected or not selected
				if (Index_row % 2 == 0 && !isCellSelected(Index_row, Index_col)) {
					comp.setBackground(tableShadeColors);
				} 
				else {
					comp.setBackground(tableColors);
				}
				return comp;
			}
			//Creates an instance of DefaultTableCellRenderer
			 DefaultTableCellRenderer renderCenter = new DefaultTableCellRenderer();

	         {//initializer block
	             renderCenter.setHorizontalAlignment(SwingConstants.CENTER);
	         }

	          @Override
	       public TableCellRenderer getCellRenderer(int row, int col) {
	       
	        	  
	              return renderCenter; // returns the object of DefaultTableCellRenderer

	       }
		};
		
		JTableHeader header = table.getTableHeader();
		header.setBackground(Color.WHITE);
		
		table.setModel(model);
		
		
		
		
		table.getTableHeader().setReorderingAllowed(false);
		table.getModel().addTableModelListener(tableHandler);
		table.setBackground(tableColors);
		
		table.setSelectionBackground(tableColors);
		table.setSelectionForeground(Color.BLACK);
		table.setGridColor(tableColors);
		table.getColumnModel().getColumn(0).setPreferredWidth(40);
		table.getColumnModel().getColumn(1).setPreferredWidth(40);
		table.getColumnModel().getColumn(2).setPreferredWidth(100);
		table.getTableHeader().setResizingAllowed(false);
		table.setBounds(22, 51, 406, 208);
		
		scrollPane = new JScrollPane(table);
		scrollPane.setSize(650,182);
		scrollPane.setBackground(tableColors);
		scrollPane.getViewport().setBackground(tableColors);
		scrollPane.setBorder(BorderFactory.createEmptyBorder());
		scrollPane.setBounds(20, 59, 406, 208);
		scrollPane.setForeground(tableColors);
		scrollPane.getVerticalScrollBar();
		scrollPane.setBounds(80, 100, 600, 400);
		add(scrollPane);
	}
	/**
	 * @return the table
	 */
	public JTable getTable() {
		return table;
	}
	/**
	 * @param table the table to set
	 */
	public void setTable(JTable table) {
		this.table = table;
	}
	/**
	 * @return the scrollPane
	 */
	public JScrollPane getScrollPane() {
		return scrollPane;
	}
	/**
	 * @param scrollPane the scrollPane to set
	 */
	public void setScrollPane(JScrollPane scrollPane) {
		this.scrollPane = scrollPane;
	}

	/**
	 * @return the model
	 */
	public DefaultTableModel getModel() {
		return model;
	}

	/**
	 * @param model the model to set
	 */
	public void setModel(DefaultTableModel model) {
		this.model = model;
	}
	/**
	 * @return the btnMainMenu
	 */
	public JButton getBtnMainMenu() {
		return btnMainMenu;
	}
	/**
	 * @param tableColors the tableColors to set
	 */
	public void setTableColors(Color tableColors) {
		this.tableColors = tableColors;
	}

}
